<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="0" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入流程名称"
            clearable
            style="margin-bottom: 20px; width: 50%; margin-right: 14px"
          />
          <el-button icon="Edit" style="margin-bottom: 20px" />
          <el-button icon="Share" style="margin-bottom: 20px" />
          <el-button icon="Delete" style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-card shadow="hover" style="margin-bottom: 20px">
            <template #header>
              <div class="card-header">
                <span>Card name</span>
              </div>
            </template>
          </el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
          <el-card shadow="hover" style="margin-bottom: 20px">Hover</el-card>
        </div>
      </el-col>
      <el-col :span="24" :xs="24">
        <!-- 查询 -->
        <el-form :model="queryParams" ref="queryRefs" :inline="true">
          <el-form-item label="流程名称" prop="noticeTitle">
            <el-input
              v-model="queryParams.flowName"
              placeholder="请输入流程名称"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 操作列 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              >新增</el-button
            >
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%; margin: 20px 0"
        >
          <!-- <el-table-column label="序号" :index="indexMethod" align="center" /> -->
          <el-table-column prop="id" v-if="false" />
          <el-table-column prop="name" label="流程名称" align="center" />
          <el-table-column prop="startUserName" label="发起人" align="center" />
          <el-table-column prop="taskName" label="节点名称" align="center" />
          <el-table-column prop="flowStatus" label="流程状态" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.flowStatus == 1">审批中</el-tag>
              <el-tag type="success" v-if="scope.row.flowStatus == 4"
                >审批通过</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="任务到达时间"
            align="center"
          />
          <el-table-column fixed="right" label="操作" align="center">
            <template #default="scope">
              <el-button
                link
                type="primary"
                size="small"
                @click="openDrawer(scope.row)"
              >
                处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="float: right"
          layout="prev, pager, next"
          v-model:page-size="queryParams.pageSize"
          v-model:current-page="queryParams.pageNum"
          :total="pageInfo.totals"
          @current-change="initData()"
        />
        <el-drawer
          title="审批详情"
          v-model="previewVisible"
          direction="rtl"
          size="35%"
          destroy-on-close
        >
          <ApprovalDetails
            :form-data="formData"
            :his-list="flowHisList"
            :proceed-tasks="proceedTasks"
            :taskId="taskId"
            :flowStatus="flowStatus"
            :processInstanceId="instanceId"
            :startUser="startUser"
            :startTime="startTime"
          ></ApprovalDetails>
          <template #footer>
            <div style="flex: auto">
              <el-button @click="handlerCancel">cancel</el-button>
              <el-button type="primary" @click="handlerSubForm">同意</el-button>
            </div>
          </template>
        </el-drawer>
      </el-col>
    </el-row>
  </div>
</template>
<script setup>
import { ref, reactive, toRefs, watch } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import {
  getFlowToDoList,
  getFlowInstanceInfo,
  flowAgree,
  flowReject,
} from "../../../api/flow.js";

const loading = ref(true);
const router = useRouter();
const tableData = ref([]);
const previewVisible = ref(false);
const formData = ref("");
const formItem = ref("");
const flowId = ref("");
const taskId = ref("");
const flowHisList = ref([]);
const proceedTasks = ref([]);
const instanceId = ref("");
const flowStatus = ref("");
const startUser = ref({});
const startTime = ref("");
import ApprovalDetails from "../approval-details.vue";
const formBuilderRef = ref(null);
const store = useStore();
const queryData = reactive({
  from: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    flowName: undefined,
  },
  rules: {
    flowName: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
  },
});

/*数据封装区域*/
let pageInfo = reactive({
  pageNum: 1,
  pageSize: 5,
  totals: Number,
});

const { from, queryParams, rules } = toRefs(queryData);
const indexMethod = (index) => {
  return index + 1;
};

const toFlowDetail = (row) => {
  router.push("/flowEdit?id=" + row.id);
};

const openDrawer = (row) => {
  let processInstanceId = row.procInstId;
  taskId.value = row.id;
  getFlowInstanceInfo({ processInstanceId,taskId:row.id })
    .then((res) => {
      if (res.status == 0) {
        formItem.value = res.data.formItem;
        formData.value = res.data.formData;
        flowHisList.value = res.data.list;
        proceedTasks.value = res.data.proceedTasks;
        instanceId.value = res.data.processInstanceId;
        flowStatus.value = res.data.flowStatus;
        startUser.value = res.data.startUser;
        startTime.value = res.data.startTime;
        store.dispatch("form/updateConfig", JSON.parse(res.data.formItem));
        previewVisible.value = true;
      }
    })
    .catch(function (error) {
      console.log(error);
    });
};

const handleAdd = () => {
  router.push("/flowAdd");
};

const initData = () => {
  loading.value = true;
  getFlowToDoList(queryParams.value).then((res) => {
    console.log(res);
    if (res.status == 0) {
      console.log(res.data);
      tableData.value = res.data.rows;
      pageInfo.totals = res.data.total;
    }
    loading.value = false;
  });
};

const handlerSubForm = () => {
  console.log("taskId", taskId.value);
  console.log("handlerSubForm", formData.value);
  flowAgree({ taskId: taskId.value, option: "" })
    .then((res) => {
      if (res.status == 0) {
        previewVisible.value = false;
        initData();
      }
    })
    .catch(function (error) {
      console.log(error);
    });
};
const handlerCancel = () => {
  console.log("taskId", taskId.value);
  console.log("handlerSubForm", formData.value);
  flowReject({ taskId: taskId.value, option: "", comment: "不同意" })
    .then((res) => {
      if (res.status == 0) {
        previewVisible.value = false;
        initData();
      }
    })
    .catch(function (error) {
      console.log(error);
    });
};

initData();

const handleQuery = () => {
  console.log("handleQuery");
};

const handleClick = () => {
  console.log("click");
};
watch(
  formData,
  (newValue, oldValue) => {
    console.log("modelValue-----------改变了", newValue);
  },
  { immediate: formData }
);
</script>
    
    <style>
.app-container {
  padding: 20px;
}
.el-drawer__header {
  margin-bottom: 14px;
}
.el-drawer__body {
  padding-top: 14px;
}
</style>